package com.lixw.saa.controller;

import com.lixw.saa.util.DateTimeTools;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.model.tool.ToolCallingChatOptions;
import org.springframework.ai.support.ToolCallbacks;
import org.springframework.ai.tool.ToolCallback;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * @ClassName: ToolCallingController
 * @Description:
 * @Author: xuweiLi
 * @Create: 2025/10/4 15:31
 **/
@RestController
public class ToolCallingController {
    @Resource
    private ChatModel chatModel;

    @Resource
    private ChatClient chatClient;

    @GetMapping("/toolcall/chat")
    public String chat(@RequestParam(name = "msg",defaultValue = "你是谁?现在几点了?") String msg) {
        ToolCallback[] toolCallbacks = ToolCallbacks.from(new DateTimeTools());
        ToolCallingChatOptions callingChatOptions = ToolCallingChatOptions.builder().toolCallbacks(toolCallbacks).build();
        Prompt prompt = new Prompt(msg, callingChatOptions);
        return chatModel.call(prompt).getResult().getOutput().getText();
    }

    @GetMapping("/toolcall/chat2")
    public Flux<String> chat2(@RequestParam(name = "msg",defaultValue = "你是谁现在几点") String msg)
    {
        return chatClient.prompt(msg)
                //.tools(new DateTimeTools())
                .stream()
                .content();
    }

}